import { _decorator, type Button, Label } from 'cc';
import { BasePopUp } from '../../../frame/component/ui.pop-up';
import { ClientAPI } from '../../api/client.api';
import GameDC from '../../manager/data.game.center';
import GameAssets from '../../manager/game.assets';
import type { Popup } from '../page/popup';
const { ccclass, property } = _decorator;

@ccclass('ppTreasureBox')
export class ppTreasureBox extends BasePopUp<typeof Popup, boolean> {
	protected default_return: boolean;

	protected initAfterOnLoad(){
		this.renderLocalUI();
	}

	public onShow(){
		GameDC.Trigger.HILIT_DIAMOND = true;
		BasePopUp.jelly_enter(this.node);
	}

	protected _click_event_: { [name: string]: (this: ppTreasureBox, button: Button) => void; } = {
		close(btn){
			btn.interactable = false;
			this.exit(false).then(()=>btn.interactable = true);
		},
		async video(btn){
			btn.interactable = false;
			await ClientAPI.AD('incentive');
			await this.exit(true);
			btn.interactable = true;
		}
	}

	private async exit(reap: boolean){
		await BasePopUp.jelly_leave(this.node);
		this.close(reap);
		GameDC.Trigger.HILIT_DIAMOND = false;
	}

	private renderLocalUI(){
		(<const>[
			['lbl-head', 39],
			['lbl-desc', 40],
			['lbl-video', 41],
			['close', 42]
		]).forEach(([name, idx])=>{
			this.node.getChildByName(name).getComponent(Label).string = GameAssets.getLangText(idx);
		});

		this.node.getChildByName('lbl-diamond').getComponent(Label).string = ' +3';
	}
}

